package com.qdxwx.data.service.system;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.system.SystemMsgMapper;
import com.qdxwx.data.mapper.user.UserLetterMapper;
import com.qdxwx.models.system.SystemMsg;
import com.qdxwx.models.user.UserLetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class SystemMsgServiceImpl implements SystemMsgService {

    private static final Logger logger = LoggerFactory.getLogger(SystemMsgServiceImpl.class);

    @Autowired
    private SystemMsgMapper systemMsgMapper;

    @Autowired
    private UserLetterMapper userLetterMapper;


    @Override
    public int insert(SystemMsg systemMsg) {
        systemMsg.setCreateTime(LocalDateTime.now());
        return systemMsgMapper.insertSystemMsg(systemMsg);
    }

    @Override
    public SystemMsg get(int id) {
        return systemMsgMapper.selectById(id);
    }

    @Override
    public List<SystemMsg> getList(SystemMsg systemMsg, PageRequest page) {
        return systemMsgMapper.selectList(systemMsg, page);
    }

    @Override
    public int count(SystemMsg systemMsg) {
        return systemMsgMapper.selectCount(systemMsg);
    }

    @Override
    public int delete(int id) {
        return systemMsgMapper.deleteById(id);
    }

    @Override
    public List<SystemMsg> getListByLastTime(LocalDateTime lastTime) {
        return systemMsgMapper.selectByLastTime(lastTime);
    }

    @Override
    public void updatePast() {
        LocalDateTime endDate = LocalDateTime.now().plusDays(-180);
        UserLetter userLetter = new UserLetter();
        userLetter.setCreateTime(endDate);
        userLetterMapper.deleteUserLetterBatch(userLetter);
        systemMsgMapper.updatePastTime(endDate.toLocalDate());
        logger.info("updatePast:" + endDate);
    }
}